package com.dayuanit.dy8.atm.springbootatm.controller;

import com.dayuanit.dy8.atm.springbootatm.dto.FlowDTO;
import com.dayuanit.dy8.atm.springbootatm.dto.ResponseDTO;
import com.dayuanit.dy8.atm.springbootatm.holder.PageHolder;
import com.dayuanit.dy8.atm.springbootatm.service.BankCardService;
import com.dayuanit.dy8.atm.springbootatm.service.FlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.util.List;

@RestController
public class BankCardController extends BaseController {

    @Autowired
    private BankCardService bankCardService;

    @Autowired
    private FlowService flowService;

    @RequestMapping(value = "/card/openAccount", method = RequestMethod.POST)
    public ResponseDTO openAccount(String pwd, String confirmPwd, HttpSession session) {
        System.out.println("密码=" + pwd);
        bankCardService.openAccount(pwd, confirmPwd, getUserId(session));
        return ResponseDTO.success();
    }

    @RequestMapping(value = "/card/deposit")
    public ResponseDTO deposit(String money, String cardNum, HttpSession session) {

        bankCardService.deposit(cardNum, money, getUserId(session));
        return ResponseDTO.success();
    }

    @RequestMapping("/card/draw")
    public ResponseDTO draw(String cardNum, String amount, String pwd, HttpSession session) {
        bankCardService.draw(cardNum, amount, pwd, getUserId(session));
        return ResponseDTO.success();
    }

    @RequestMapping("/card/getFlows")
    public ResponseDTO getFlows(String cardNum, String pwd, @RequestParam(required = false, defaultValue = "1") int currentPage, HttpSession session) {
        return ResponseDTO.success(bankCardService.listFlows(cardNum, pwd, getUserId(session), currentPage));
    }

    @RequestMapping("/card/transfer")
    public ResponseDTO transfer(String outCardNum, String inCardNum, String pwd, String amount, HttpSession session) {
//        bankCardService.transfer(outCardNum, inCardNum, pwd, amount, getUserId(session));
//        bankCardService.transferDelay(outCardNum, inCardNum, pwd, amount, getUserId(session));
        //>>>>>>>>>>>>>>>bankCardService=com.dayuanit.atm.service.BankCardService$$EnhancerBySpringCGLIB$$677b4057
        System.out.println(">>>>>>>>>>>>>>>bankCardService=" + bankCardService.getClass().getName());
        bankCardService.transferDelay(outCardNum, inCardNum, pwd, amount, getUserId(session));
        return ResponseDTO.success();
    }

    @RequestMapping("/card/getBankCards")
    public ResponseDTO getBankCards(HttpSession session) {
        return ResponseDTO.success(bankCardService.listCards(getUserId(session)));
    }

    @RequestMapping("/card/getFlowTop10")
    public ResponseDTO getFlowTop10(HttpSession session) {
        return ResponseDTO.success(flowService.listTop10(getUserId(session)));
    }

    @RequestMapping("/card/downFlow")
    public void downFlow(String cardNum, String pwd, HttpSession session, HttpServletResponse response) {
        int currentPage = 1;
        PageHolder pageHolder = null;
        response.setContentType("application/octet-stream; charset=utf-8");
        //文件名如果有中文 通过URLEncoder.encode(filename, "UTF-8")转码
        response.setHeader("content-disposition", "attachment;filename=" + cardNum + ".csv");
        try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), "utf-8"));){
            bw.write("卡号,金额,备注,时间");
            bw.newLine();
            bw.flush();
            System.out.println("abc \"\" ");

            do {
                pageHolder = bankCardService.listFlows(cardNum, pwd, getUserId(session), currentPage);
                System.out.println("currentPage=" + currentPage);
                currentPage ++;

                Object obj = pageHolder.getData();
                if (null == obj) {
                    continue;
                }
                List<FlowDTO> list = (List<FlowDTO>)obj;

                for (FlowDTO dto : list) {
                    String msg = dto.getCardNum() + "," + dto.getAmount() + "," + dto.getDesc() + "," + dto.getCreateTime();
                    bw.write(msg);
                    bw.newLine();
                    bw.flush();
                }

            } while (pageHolder.getTotalPage() != 0 && currentPage <= pageHolder.getTotalPage());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
